package dbx

import (
	"database/sql"
	"fmt"
	_ "github.com/go-sql-driver/mysql"
)

type Engin struct {
	*sql.DB
}

func NewEngin(host string, port int, user, password string) (*Engin, error) {
	return NewEnginWithDB(host, port, user, password, "mysql")
}

func NewEnginWithDB(host string, port int, user string, password string, database string) (*Engin, error) {
	connStr := fmt.Sprintf("%s:%s@tcp(%s:%d)/%v?interpolateParams=true", user, password, host, port, database)
	db, err := sql.Open("mysql", connStr)
	if err != nil {
		return nil, err
	}
	err = db.Ping()
	if err != nil {
		return nil, err
	}
	return &Engin{
		DB: db,
	}, nil
}
